from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, ForeignKey, JSON, Float
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.core.database import Base

class UserBehavior(Base):
    """用户行为模型"""
    __tablename__ = "user_behaviors"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, comment="用户ID")
    session_id = Column(String(100), nullable=True, comment="会话ID")
    
    # 行为信息
    action = Column(String(100), nullable=False, comment="操作类型")
    module = Column(String(50), nullable=False, comment="模块名称")
    resource = Column(String(100), nullable=True, comment="资源标识")
    description = Column(Text, nullable=True, comment="操作描述")
    
    # 请求信息
    ip_address = Column(String(45), nullable=True, comment="IP地址")
    user_agent = Column(Text, nullable=True, comment="用户代理")
    device_type = Column(String(20), nullable=True, comment="设备类型")  # desktop, mobile, tablet
    browser = Column(String(50), nullable=True, comment="浏览器")
    os = Column(String(50), nullable=True, comment="操作系统")
    
    # 性能信息
    response_time = Column(Float, nullable=True, comment="响应时间(毫秒)")
    status_code = Column(Integer, nullable=True, comment="状态码")
    
    # 位置信息
    country = Column(String(50), nullable=True, comment="国家")
    city = Column(String(100), nullable=True, comment="城市")
    
    # 元数据
    meta_data = Column(JSON, nullable=True, comment="行为元数据")
    
    # 时间字段
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    
    # 关系
    user = relationship("User", back_populates="behaviors")
    
    def __repr__(self):
        return f"<UserBehavior(id={self.id}, user_id={self.user_id}, action='{self.action}')>"

class PageView(Base):
    """页面访问模型"""
    __tablename__ = "page_views"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=True, comment="用户ID")
    session_id = Column(String(100), nullable=False, comment="会话ID")
    
    # 页面信息
    page_url = Column(String(500), nullable=False, comment="页面URL")
    page_title = Column(String(200), nullable=True, comment="页面标题")
    referrer = Column(String(500), nullable=True, comment="来源页面")
    
    # 访问信息
    duration = Column(Integer, nullable=True, comment="停留时间(秒)")
    bounce = Column(Boolean, default=False, comment="是否跳出")
    
    # 设备信息
    device_type = Column(String(20), nullable=True, comment="设备类型")
    screen_resolution = Column(String(20), nullable=True, comment="屏幕分辨率")
    viewport_size = Column(String(20), nullable=True, comment="视口大小")
    
    # 位置信息
    ip_address = Column(String(45), nullable=True, comment="IP地址")
    country = Column(String(50), nullable=True, comment="国家")
    city = Column(String(100), nullable=True, comment="城市")
    
    # 时间字段
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    
    def __repr__(self):
        return f"<PageView(id={self.id}, page_url='{self.page_url}', user_id={self.user_id})>"

class UserSession(Base):
    """用户会话模型"""
    __tablename__ = "user_sessions"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=True, comment="用户ID")
    session_id = Column(String(100), unique=True, index=True, nullable=False, comment="会话ID")
    
    # 会话信息
    start_time = Column(DateTime(timezone=True), server_default=func.now(), comment="开始时间")
    end_time = Column(DateTime(timezone=True), nullable=True, comment="结束时间")
    duration = Column(Integer, nullable=True, comment="会话时长(秒)")
    page_views = Column(Integer, default=0, comment="页面浏览数")
    
    # 设备信息
    device_type = Column(String(20), nullable=True, comment="设备类型")
    browser = Column(String(50), nullable=True, comment="浏览器")
    os = Column(String(50), nullable=True, comment="操作系统")
    
    # 位置信息
    ip_address = Column(String(45), nullable=True, comment="IP地址")
    country = Column(String(50), nullable=True, comment="国家")
    city = Column(String(100), nullable=True, comment="城市")
    
    # 会话状态
    is_active = Column(Boolean, default=True, comment="是否活跃")
    
    def __repr__(self):
        return f"<UserSession(id={self.id}, session_id='{self.session_id}', user_id={self.user_id})>"